import { actionType } from "./action.js";

const initialState = {
  count:0,
};
export default function counter(state = initialState, action) {
  console.log("Reducer called with action:", state,action);

  //从 action 中解构出 type 和 data
  const { type, data } = action;

  // 根据 action 的 type 执行不同的操作
  let count = 0;
  switch (type) {
    case actionType.INCREMENT:
      count = state.count + data;
      break;
    case actionType.DECREMENT:
      count = state.count - data;
      break;
    case actionType.RESET:
       count = 0;
       break;
    default:
      //return state.count;
  }
  console.log("Reducer state:", state);
  return {
    ...state,
    count,
  } ;
}
